package com.gec.servlet;

import java.io.IOException;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.gec.entity.Cart;
import com.gec.entity.CartItem;
@WebServlet("/DeleteCartItemServlet")
public class DeleteCartItemServlet extends HttpServlet{
	 protected void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
	      
		  	String pid = request.getParameter("pid");
		  	HttpSession session = request.getSession();
		  	Cart cart = (Cart) session.getAttribute("cart");
		  	Map<String, CartItem> cartItems = cart.getCartItems();
		  	cartItems.remove(pid);
		      //如果购物车中没有商品，则清空购物车
		  	if (cartItems.size() == 0) {
		  	    session.removeAttribute("cart");
		  	}
		  
		  	response.sendRedirect("cart.jsp");
		      }
}
